import Admini from '@/views/admin/Admin.vue'
import RBACView from '@/views/admin/RBACView.vue'
import type { RouteRecordRaw } from 'vue-router'
import { useUserStore } from '@/stores/auth/user'
// 管理员相关的路由配置
export const adminRoutes: RouteRecordRaw[] = [
    {
        path: '/admin-dashboard',
        name: 'admin',
        component: Admini,
        meta: {
            title: '管理员后台',
            requiresAuth: true,
            roles: ['ADMIN', 'SUPER_ADMIN'] // 仅允许管理员访问
        },
        beforeEnter:(to)=>{
            const userStore = useUserStore()
            const requiresAuth = to.meta?.requiresAuth
            const allowedRoles = (to.meta?.roles as string[]) || []

             if (requiresAuth && !userStore.isLoggedIn) {
               return { path: '/login', query: { redirect: to.fullPath } }
             }

            // 已登录但角色不在允许列表
            if (allowedRoles.length && !allowedRoles.includes(userStore.getRole())) {
                // 没有 403 页面可改为返回首页
                return { path: '/' } // 或者 { path: '/' }
            }
        }
    },
    {
        path: '/admin/rbac',
        name: 'admin-rbac',
        component: RBACView,
        meta: {
            title: '角色管理',
            requiresAuth: true,
            roles: ['ADMIN', 'SUPER_ADMIN']
        },
        beforeEnter:(to)=>{
            const userStore = useUserStore()
            const requiresAuth = to.meta?.requiresAuth
            const allowedRoles = (to.meta?.roles as string[]) || []
            if (requiresAuth && !userStore.isLoggedIn) {
              return { path: '/login', query: { redirect: to.fullPath } }
            }
            if (allowedRoles.length && !allowedRoles.includes(userStore.getRole())) {
                return { path: '/' }
            }
        }
    }
]